package com.example.demo.auxiliary;

import com.example.demo.identity.User;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

// 这是一个处理sql的辅助类
public class UserSqlProvider {

    private final static Logger logger = LoggerFactory.getLogger(UserSqlProvider.class);

    public String updateUserBasicInfo(@Param("uuId") String uuId, @Param("user") User user) {

        if (uuId == null || uuId.isEmpty()) {
            throw new IllegalArgumentException("uuId cannot be null or empty");
        }

        StringBuilder sql = new StringBuilder("UPDATE common SET ");
        List<String> fields = new ArrayList<>();

        if (user.getUsername() != null && !user.getUsername().isEmpty()) {
            fields.add("`username` = #{user.username}");
        }
        if (user.getAvatar() != null && !user.getAvatar().isEmpty()) {
            fields.add("`avatar` = #{user.avatar}");
        }
        if (user.getGex() != null && !user.getGex().isEmpty()) {
            fields.add("`gex` = #{user.gex}");
        }
        if (user.getSignatureOfTheIndividual() != null && !user.getSignatureOfTheIndividual().isEmpty()) {
            fields.add("`signatureOfTheIndividual` = #{user.signatureOfTheIndividual}");
        }
        if (user.getBirthday() != null && !user.getBirthday().isEmpty()) {
            fields.add("`birthday` = #{user.birthday}");
        }
        if (user.getProvince() != null && !user.getProvince().isEmpty()) {
            fields.add("`province` = #{user.province}");
        }
        if (user.getCity() != null && !user.getCity().isEmpty()) {
            fields.add("`city` = #{user.city}");
        }

        if (fields.isEmpty()) {
            throw new IllegalArgumentException("No fields to update");
        }

        sql.append(String.join(", ", fields));
        sql.append(" WHERE `uu_id` = #{uuId}");

        logger.debug("Generated SQL: {}", sql.toString());

        return sql.toString();

    }
}